package com.quanquan.user.message;

import com.quanquan.user.service.UserService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName MqUserReceiver
 * @Description
 * @Author Owen
 * @Date 2020/2/6 0006 下午 6:44
 * @Version V1.0
 **/
@Slf4j
@Component
public class MqUserReceiver {

    @Autowired
    private UserService userService;

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "userQueue",
                    durable = "true"),
            exchange = @Exchange(value = "userExchange",
                    type = "topic"),
            ignoreDeclarationExceptions = "true",
            key = "updateTime")})
    @RabbitHandler
    public void updateUserLastTime(String openid, Channel channel, @Headers Map<String,Object> headers){
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            log.error("确认标签消息[{}]失败,原因：{}", openid, e.getMessage());
        }
        log.info("updateTime消费者：{}",openid);
        userService.updateUserUpdateTime(openid);
    }

}
